package com.spring.shops.admin.controller.product;
import com.spring.shops.model.dto.ProdDTO;
import com.spring.shops.model.dto.SaveProductDTO;
import com.spring.shops.model.result.IPageIndexImg;
import com.spring.shops.model.result.ServerResponseEntityVoid;
import com.spring.shops.model.vo.ProductVO;
import com.spring.shops.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

//产品管理->产品模块
@RestController
@RequestMapping(value = "/prod/prod",produces = "application/json; charset=utf-8")
public class ProductController {

    @Autowired
    private ProductService service;
    /**
     * 产品分页查询
     * @param prodDTO
     * @return
     */
    @GetMapping("/page")
    public ServerResponseEntityVoid<IPageIndexImg<ProductVO>> page(ProdDTO prodDTO){

        return service.page(prodDTO);
    }

    /**
     * 保存商品数据
     * @param dto
     * @return
     */
    @PostMapping
    public ServerResponseEntityVoid save(@RequestBody SaveProductDTO dto){
        System.out.println(dto);
        boolean flag=service.save(dto);
        if (flag){
            return ServerResponseEntityVoid.success();
        }else return null;
    }

    /**
     * 根据ids批量删除数据
     * @param integers
     * @return
     */
    @DeleteMapping
    public  ServerResponseEntityVoid deleteByIds(@RequestBody List<Long> integers){
        boolean flag=service.deleteByProdIds(integers);
        if (flag){
            return ServerResponseEntityVoid.success();
        }else return null;
    }

    /**
     * 根据产品id获取信息
     * @param prodId
     * @return
     */
    @GetMapping("/info/{prodId}")
    public ServerResponseEntityVoid<ProductVO> getInfoById(@PathVariable Integer prodId){
          return service.getInfoById(prodId);
    }

    /**
     * 更新数据
     * @param dto
     * @return
     */
    @PutMapping
    public ServerResponseEntityVoid updateInfo(@RequestBody SaveProductDTO dto){
        boolean flag=service.updateInfo(dto);
        if (flag){
            return ServerResponseEntityVoid.success();
        }else return null;
    }



}
